import { Component, OnInit, Injector, ViewChild } from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { Table } from 'primeng/table';
import { Paginator, LazyLoadEvent } from 'primeng/primeng';
import {
    ComboboxItemDto,
    ListDtoExtensionsItem,
    StockType,
    StockZT_ToolingAccountServiceProxy,
    Stock_ToolingAccountListDto,
    StockZT_RoomServiceProxy,
    Stock_BillListDto,
    Stock_LostToolingItemListDto,
    StockZT_LostBillItemServiceProxy,
    SessionServiceProxy
} from '@shared/service-proxies/stock-service-proxies';
import { PrimengTableHelper } from '@shared/helpers/PrimengTableHelper';
import { UserDataPermissionServiceProxy } from '@shared/service-proxies/stock-service-proxies';
import { AppConsts } from '@shared/AppConsts';

@Component({
    templateUrl: './tooling-lost-account.component.html',
    animations: [appModuleAnimation()]
})
export class ToolingLostAccountComponent extends AppComponentBase implements OnInit {
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('detailDataTable') detailDataTable: Table;
    @ViewChild('paginator') paginator: Paginator;
    @ViewChild('detailPaginator') detailPaginator: Paginator;
    // filters
    f_MaterialCode = '';
    f_MaterialName = '';
    f_CAL_ToolingPictureId = '';
    f_Station = '';
    f_TecCondition = '';
    f_Specification = '';
    f_QualityLevel = '';
    f_InventBatchID = '';
    f_WarehouseName = '';
    f_RealInventory;
    f_CAL_ProductModel = '';  
    f_SeriousNum;
    userDataPermissions;
    selectedDepts;
    extensions: Array<ListDtoExtensionsItem>;
    useableOptions = [{ label: '全部', value: undefined }, { label: '可用', value: true }];
    isuseable;
    scrollHeight = '250px';
    roomOption;
    selectedWareHouse;
    selectedBill: Stock_BillListDto;
    detailPrimengTableHelper: PrimengTableHelper;
    detialExtensions: Array<ListDtoExtensionsItem>;
    selectedBillItem = new Stock_LostToolingItemListDto();
    materialCodeFilter = '';
    billcode = '';
    selectedAccounts;
    billStatusOptions = [
        { label: '全部', value: undefined },
        { label: '新建', value: '新建' },
        { label: '已提交', value: '已提交' },
        { label: '已出库', value: '已出库' }
    ];
    billstatus = '';
    constructor(
        injector: Injector,
        private _accountService: StockZT_ToolingAccountServiceProxy,
        private _lostService: StockZT_LostBillItemServiceProxy,
        private _userDataPermissionServiceProxy: UserDataPermissionServiceProxy,
        private _stockZT_RoomServiceProxy: StockZT_RoomServiceProxy,
        private _base_SessionServiceProxy: SessionServiceProxy
    ) {
        super(injector);
    }

    ngOnInit(): void {
        debugger
        this._userDataPermissionServiceProxy.getUserDataPermissions('Warehouses').subscribe(result => {
            this._lostService.getWarehouseCombobox('工装库').subscribe(result2 => {
                this.roomOption = result2.items.map(e => {
                    return {
                        label: e.displayText,
                        value: e
                    };
                });
                this.selectedWareHouse = result2.items;
                this.getAccounts();
            });
        });

        this.scrollHeight = document.body.clientHeight - 490 + 'px';
        this._userDataPermissionServiceProxy.getUserDataPermissions('Tooling_Accounts').subscribe(result => {
            this.userDataPermissions = result.items.map(e => {
                return {
                    label: e.displayText,
                    value: e
                };
            });
             this._base_SessionServiceProxy.getCurrentDept().subscribe(result => {             let ssData = new ComboboxItemDto();             ssData.value = result.value;             ssData.isSelected = false;             ssData.displayText = result.label;             let ss: ComboboxItemDto[] = [ssData];             this.selectedDepts = ss;         });
            this.getAccounts();
            // if (this.userDataPermissions.length > 0) {
            //     this.selectedDepts = this.userDataPermissions[0].value;
            //     this.getAccounts();
            // }
        });
    }

    printBillList() {
        if (this.selectedDepts === null) {
            this.notify.error('请选择需要打印的单据！');
        } else {
            //装配类派工单
            window.open(
                AppConsts.printAddr + '/webroot/decision/view/report?viewlet=erp/XHWZTZ.cpt&Dept=' +
                    this.selectedDepts.displayText
            );
        }
    }
    getAccounts(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event) || event.first == undefined) {
            this.paginator.changePage(0);

            return;
        }

        this.primengTableHelper.showLoadingIndicator();
        this._lostService
            .getLostToolingListExtension(
                undefined,
                this.selectedWareHouse,
                this.materialCodeFilter,
                this.billcode,
                this.billstatus,
                '',
                this.f_Specification,
                this.f_CAL_ToolingPictureId,
                this.f_CAL_ProductModel,
                this.f_SeriousNum,
                this.primengTableHelper.getSorting(this.dataTable),
                this.primengTableHelper.getMaxResultCount(this.paginator, event),
                this.primengTableHelper.getSkipCount(this.paginator, event)
            )
            .subscribe(result => {
                this.extensions = result.items;
                this.primengTableHelper.totalRecordsCount = result.stock_LostToolingList.totalCount;
                this.primengTableHelper.records = result.stock_LostToolingList.items;
                this.primengTableHelper.hideLoadingIndicator();
                this.selectedBillItem = null;
            });
      
        //this._accountService;
        //this._accountService
        //    .getScrapToolingAccountListExtension(
        //        this.f_MaterialCode,
        //        this.f_MaterialName,
        //        this.f_RealInventory,
        //        this.f_SeriesNum,
        //        this.selectedWareHouse,
        //        this.f_Specification,
        //        this.f_TradeMark,
        //        this.f_Precision,
        //        //  this.selectedDepts.value,
        //        '',
        //        this.isuseable,
        //        StockType.工装,
        //        false,
        //        this.primengTableHelper.getSorting(this.dataTable),
        //        this.primengTableHelper.getMaxResultCount(this.paginator, event),
        //        this.primengTableHelper.getSkipCount(this.paginator, event)
        //    )
        //    .subscribe(result => {
        //        this.extensions = result.items;
        //        this.primengTableHelper.totalRecordsCount = result.dtos.totalCount;
        //        this.primengTableHelper.records = result.dtos.items;
        //        this.primengTableHelper.hideLoadingIndicator();
        //    });
    }
}
